import {
  AmcaxRender,
  EntityInfo,
  EntityManager,
  EntityPartType,
  InteractionManager,
} from '@amcax/web-render';
import { useRenderStore } from '../store';
import { ManagerId } from 'nextcae-base';
export enum EntOtpMod {
  HOVER = 'setEntityHovered',
  SELECTED = 'setEntitySelected',
}
export class RenderBase {
  get pickType() {
    return this.interactionMgr.pickType;
  }
  get mainRenderer(): AmcaxRender {
    return useRenderStore.getState().mainRenderer;
  }
  get camera() {
    return this.mainRenderer.cameraManager.camera;
  }
  get controls() {
    return this.mainRenderer.cameraManager.controls;
  }

  /**
   * 渲染模型的HTMLELement
   */
  get container() {
    return this.mainRenderer.container;
  }
  get geometryMgr(): EntityManager {
    return this.mainRenderer.getEntityManager(ManagerId.Geometry);
  }
  get meshMgr(): EntityManager {
    return this.mainRenderer.getEntityManager(ManagerId.Mesh);
  }
  get assisMgr(): EntityManager {
    return this.mainRenderer.getEntityManager(ManagerId.Assis);
  }
  get interactionMgr(): InteractionManager {
    return this.mainRenderer.interactionManager;
  }
  get enableBoxSelect() {
    return this.interactionMgr.enableBoxSelect;
  }
  set enableBoxSelect(value: boolean) {
    this.interactionMgr.enableBoxSelect = value;
  }
  set enablePick(value: boolean) {
    this.interactionMgr.enablePick = value;
  }
  get pickFirstOnly() {
    return this.interactionMgr.pickFirstOnly;
  }
  set pickFirstOnly(value: boolean) {
    this.interactionMgr.pickFirstOnly = value;
  }
  set pickType(type: EntityPartType[] | EntityPartType) {
    this.interactionMgr.pickType = type;
  }
  /**
   * 进行选中、设置颜色等渲染相关的操作后，都需要主动调用一次render
   * */
  render() {
    this.mainRenderer.render();
  }
  getMgr(mId = ManagerId.Geometry) {
    const key = mId === ManagerId.Mesh ? 'meshMgr' : 'geometryMgr';
    return this[key];
  }
  setEntsColor(color: number, entType: EntityPartType, ids: number[]) {
    this.geometryMgr.setEntityColor(color, undefined, entType, ids);
  }
  setEntityVisible(
    entType: EntityPartType,
    visible: boolean,
    ids: number[] | undefined,
    mId = ManagerId.Geometry,
  ) {
    if (mId === ManagerId.Mesh) {
      this.meshMgr.setEntityVisible(visible, undefined, entType, ids);
    } else if (mId === ManagerId.Geometry) {
      this.geometryMgr.setEntityVisible(visible, undefined, entType, ids);
    }
    this.mainRenderer.render();
  }
  findRelatedEntityInfos(ent: EntityInfo, mId = ManagerId.Geometry) {
    return this.getMgr(mId)?.findRelatedEntityInfos(
      ent?.dxid,
      ent?.type,
      ent?.idx,
    );
  }
  /**
   * 处理实体的选择或悬停状态
   * @param ent - 实体信息对象
   * @param state -  是否为悬停或选择状态
   * @param key - 实体操作模式，用于指定是悬停还是选择操作
   */
  setSelectOrHover(
    ent: EntityInfo,
    state = false,
    key = EntOtpMod.SELECTED,
    mId = ManagerId.Geometry,
  ) {
    if (key === EntOtpMod.SELECTED) {
      this.getMgr(mId)?.setEntitySelected(state, ent.dxid, ent.type, ent.idx);
    } else {
      this.getMgr(mId)?.setEntityHovered(state, ent.dxid, ent.type, ent.idx);
    }
  }
  clearSelected() {
    this.interactionMgr.clearSelected();
  }
  clearAssisMgr() {
    this.assisMgr.dispose();
  }
  /**
   * @param {number} num - 用于计算默认尺寸位置的数值，默认为 80。
   * @returns {number} - 返回 num 除以相机缩放比例的结果
   */
  public getLenByZoom(num = 40) {
    return num / this.camera.zoom; // 返回 num 除以相机缩放比例的结果
  }
}
